//
//  HMStatusViewModel.m
//  weibo
//
//  Created by apple on 2016/11/17.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "HMStatusViewModel.h"

@implementation HMStatusViewModel

-(NSString *)showTime{
//NSString *strtext =@"Nov 19 10:53:06 +0800 2016";
    NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
    dateformate.dateFormat = @"EEE MMM d HH:mm:ss Z yyyy";
    
   NSDate *sourceDate = [dateformate dateFromString:self.status.created_at];
    if (sourceDate == nil) {
        return @"走好";
    }
    NSDate *currentDate = [NSDate new];
    NSCalendar *calendar = [NSCalendar currentCalendar];
  NSDateComponents *dateComp =  [calendar components:NSCalendarUnitYear fromDate:sourceDate toDate:currentDate options:0];
//    NSLog(@"%@",sourceDate);
    if (dateComp.year == 0 ) {
        if([calendar isDateInToday:sourceDate]){
            NSTimeInterval count = [currentDate timeIntervalSinceDate:sourceDate];
        if (count < 60 ) {
            return @"刚刚";
        }else if (count < 3600){
            NSInteger num = count / 60;
            return [NSString stringWithFormat:@"%zd分钟之前",num];
        }else{
            NSInteger num = count / 3600;
            return [NSString stringWithFormat:@"%zd小时之前",num];
        }
        }else if ([calendar isDateInYesterday:sourceDate]){
            dateformate.dateFormat = @"昨天 HH:mm";
            return [dateformate stringFromDate:sourceDate];
        }
    }else{
        dateformate.dateFormat = @"yyyy-MM-dd HH:mm";
        return [dateformate stringFromDate:sourceDate];
    }
    return @"hao";
}

-(NSArray<HMPictuerInfo *> *)pictures{

    if (self.status.retweeted_status == nil) {
//                for (int i = 0; i < _status.pic_urls.count; i++) {
//                    HMPictuerInfo *ss = _status.pic_urls[i];
//        
//                    NSLog(@"%@",ss.url_pic);
//        
//                }
        return self.status.pic_urls;
    }
    
//    for (int i = 0; i < _status.retweeted_status.pic_urls.count; i++) {
//        HMPictuerInfo *ss = _status.retweeted_status.pic_urls[i];
//        
//        NSLog(@"retweeted_status  %@",ss.url_pic);
//        
//    }

//    NSLog(@"%@",self.status..pic_urls);
//    return self.status.retweModel.pic_urls;
    return self.status.retweeted_status.pic_urls;
}

-(void)setStatus:(HMStatusModel *)status{
    _status = status;
//    self.user = (HMUserInfoModel*)status.user;
    [self changerTourl];
    [self dealmymbRank];
    [self dealverified_type];
 self.sourceP = [self clearTheSourceStr];
}
-(NSString*)clearTheSourceStr{
//    <a href="http://app.weibo.com/t/feed/2J8wRB" rel="nofollow">iPhone 7</a>
    NSString *str = self.status.source;
//    NSString *str = @"dasd";
   NSRange start =  [str rangeOfString:@"\">"];
   NSRange end = [str rangeOfString:@"</a>"];
    if (!start.length && !end.length) {
        return @"来自未来的你";
    }     
    NSInteger len = end.location - (start.location + start.length);
    NSRange sou = NSMakeRange(start.location +start.length, len);
    NSString *result = [str substringWithRange:sou];
    return result;
}
-(void)changerTourl{
//    _url = [NSURL URLWithString:self.status.usermodel.avatar_large];
    _url = [NSURL URLWithString:self.status.user.avatar_large];
}
-(void)dealmymbRank{
//    self.img_mbRank = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%zd",self.status.usermodel.mbrank.integerValue]];
    self.img_mbRank = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%zd",self.status.user.mbrank.integerValue]];

}
-(void)dealverified_type{

    switch (self.status.user.verified_type.intValue) {
        case 0:
            self.verified_type = [UIImage imageNamed:@"avatar_vip"];
            break;
        case 2:case 3:case 5:
            self.verified_type = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
        case 220:
            self.verified_type = [UIImage imageNamed:@"avatar_grassroot"];
            break;
        default:
            self.verified_type = nil;
            break;
    }
    
    
}
@end
